package com.supreme.cacheserver.controller;

import com.alibaba.fastjson.JSONObject;
import com.supreme.common.dto.OmsDept;
import com.supreme.common.dto.OmsUser;
import com.supreme.common.dto.ResString;
import com.supreme.commonboot.utils.AjaxResp;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import javax.annotation.Resource;
import java.net.URI;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@RestController
@RequestMapping(value = "oms-user")
public class OmsUserController {

    @Resource
    private RestTemplate restTemplate;

    @Resource
    private RedisTemplate<String, OmsUser> redisTemplate;

    @GetMapping(value = "getOmsUserList")
    public AjaxResp getOmsUserList() {
        CompletableFuture<List<OmsUser>> completableFuture = CompletableFuture.supplyAsync(() -> {
            URI requestUri = UriComponentsBuilder.fromHttpUrl("http://localhost:9633/omni/test/biz/sysUserList")
                    .build().encode().toUri();
            String stringResponse = restTemplate.getForObject(requestUri, String.class);
            return stringResponse;
        }).thenApply(s -> {
            ResString resString = JSONObject.parseObject(s, ResString.class);
            OmsUser[] omsUserArray = JSONObject.parseObject(resString.getData(), OmsUser[].class);
            return Stream.of(omsUserArray).collect(Collectors.toList());
        }).whenComplete((omsUserList, throwable) -> {
            System.out.println("omsUserList = " + omsUserList);
        });

        List<OmsUser> omsUserList;
        try {
            omsUserList = completableFuture.get();
        } catch (InterruptedException | ExecutionException e) {
            throw new RuntimeException(e);
        }

        return AjaxResp.instance(omsUserList);
    }

    @PostMapping(value = "setOmsUserListToRedis")
    public AjaxResp setOmsUserListToRedis() {
        CompletableFuture<List<OmsUser>> completableFuture = CompletableFuture.supplyAsync(() -> {
            URI requestUri = UriComponentsBuilder.fromHttpUrl("http://localhost:9633/omni/test/biz/sysUserList")
                    .build().encode().toUri();
            String stringResponse = restTemplate.getForObject(requestUri, String.class);
            return stringResponse;
        }).thenApply(s -> {
            ResString resString = JSONObject.parseObject(s, ResString.class);
            return JSONObject.parseObject(resString.getData(), OmsUser[].class);
        }).handle((omsUserList, throwable) -> {
            redisTemplate.delete("USER_LIST");
            redisTemplate.opsForList().rightPushAll("USER_LIST", omsUserList);
            return Stream.of(omsUserList).collect(Collectors.toList());
        });

        List<OmsUser> omsUserList;
        try {
            omsUserList = completableFuture.get();
        } catch (InterruptedException | ExecutionException e) {
            throw new RuntimeException(e);
        }

        return AjaxResp.instance(omsUserList);
    }
}
